


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: AbstractContainerConverter</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.convert.vienna.base</a> ]
</div>

<h1>Coverage Summary for Class: AbstractContainerConverter (at.schauer.gregor.vienna.convert.vienna.base)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">AbstractContainerConverter</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    92.9%
  </span>
  <span class="absValue">
    (13/ 14)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    93.8%
  </span>
  <span class="absValue">
    (60/ 64)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.convert.vienna.base;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContextAware;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.serializer.Serializer;
<i>22</i>&nbsp;import at.schauer.gregor.vienna.util.ViennaUtils;
<i>23</i>&nbsp;import org.apache.commons.lang3.ClassUtils;
<i>24</i>&nbsp;import org.springframework.beans.BeanUtils;
<i>25</i>&nbsp;
<i>26</i>&nbsp;import java.util.ArrayList;
<i>27</i>&nbsp;import java.util.HashMap;
<i>28</i>&nbsp;import java.util.Iterator;
<i>29</i>&nbsp;import java.util.List;
<i>30</i>&nbsp;import java.util.regex.Pattern;
<i>31</i>&nbsp;
<i>32</i>&nbsp;/**
<i>33</i>&nbsp; * @author Gregor Schauer
<i>34</i>&nbsp; */
<b class="nc"><i>35</i>&nbsp;public abstract class AbstractContainerConverter&lt;C, E&gt; implements SeparatorConverter&lt;C&gt;, ViennaContextAware {</b>
<b class="fc"><i>36</i>&nbsp;	protected static final Pattern INDICES_PATTERN = Pattern.compile(&quot;\\[[\\d,]*\\].{2,}&quot;);</b>
<b class="fc"><i>37</i>&nbsp;	private static final Pattern SEPARATOR_PATTERN = Pattern.compile(&quot;,&quot;);</b>
<i>38</i>&nbsp;	protected String containerBegin;
<i>39</i>&nbsp;	protected String containerEnd;
<i>40</i>&nbsp;	protected ViennaContext ctx;
<i>41</i>&nbsp;
<b class="fc"><i>42</i>&nbsp;	public AbstractContainerConverter(String containerBegin, String containerEnd) {</b>
<b class="fc"><i>43</i>&nbsp;		this.containerBegin = containerBegin;</b>
<b class="fc"><i>44</i>&nbsp;		this.containerEnd = containerEnd;</b>
<b class="fc"><i>45</i>&nbsp;	}</b>
<i>46</i>&nbsp;
<i>47</i>&nbsp;	@Override
<i>48</i>&nbsp;	public C sourceToTarget(String source, Class&lt;? extends C&gt; targetClass) {
<b class="fc"><i>49</i>&nbsp;		source = stripContainerSequences(source);</b>
<b class="fc"><i>50</i>&nbsp;		String[] split = split(source, getSeparator());</b>
<b class="fc"><i>51</i>&nbsp;		C instance = createInstance(targetClass, split.length);</b>
<b class="fc"><i>52</i>&nbsp;		for (int i = 0; i &lt; split.length; i++) {</b>
<b class="fc"><i>53</i>&nbsp;			add(instance, i, this.&lt;E&gt;deserialize(split[i]));</b>
<i>54</i>&nbsp;		}
<b class="fc"><i>55</i>&nbsp;		return instance;</b>
<i>56</i>&nbsp;	}
<i>57</i>&nbsp;
<i>58</i>&nbsp;	protected abstract void add(C instance, int i, E elem);
<i>59</i>&nbsp;
<i>60</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>61</i>&nbsp;	protected C createInstance(Class&lt;? extends C&gt; targetClass, int size) {
<b class="fc"><i>62</i>&nbsp;		Serializer&lt;C&gt; serializer = (Serializer&lt;C&gt;) ctx.getSerializer(targetClass);</b>
<b class="fc"><i>63</i>&nbsp;		if (serializer != null) {</b>
<b class="fc"><i>64</i>&nbsp;			return serializer.load(new HashMap&lt;String, Object&gt;(), targetClass);</b>
<i>65</i>&nbsp;		} else {
<b class="fc"><i>66</i>&nbsp;			return BeanUtils.instantiateClass(targetClass);</b>
<i>67</i>&nbsp;		}
<i>68</i>&nbsp;	}
<i>69</i>&nbsp;
<i>70</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>71</i>&nbsp;	protected &lt;E&gt; E deserialize(String str) {
<i>72</i>&nbsp;		try {
<b class="fc"><i>73</i>&nbsp;			String type = ViennaUtils.ConverterUtils.typeFromString(str);</b>
<b class="fc"><i>74</i>&nbsp;			String value = ViennaUtils.ConverterUtils.valueFromString(str);</b>
<b class="fc"><i>75</i>&nbsp;			Class&lt;E&gt; clazz = (Class&lt;E&gt;) ClassUtils.getClass(type);</b>
<b class="fc"><i>76</i>&nbsp;			return (E) ViennaUtils.DeserializationUtils.load(ctx, clazz, value);</b>
<b class="nc"><i>77</i>&nbsp;		} catch (ClassNotFoundException e) {</b>
<b class="nc"><i>78</i>&nbsp;			throw new ViennaException(e);</b>
<i>79</i>&nbsp;		}
<i>80</i>&nbsp;	}
<i>81</i>&nbsp;
<i>82</i>&nbsp;	protected final String[] split(String str, String separator) {
<b class="fc"><i>83</i>&nbsp;		if (!INDICES_PATTERN.matcher(str).matches()) {</b>
<b class="fc"><i>84</i>&nbsp;			str = stripContainerSequences(str);</b>
<b class="fc"><i>85</i>&nbsp;			return str.isEmpty() ? new String[0] : str.split(separator);</b>
<i>86</i>&nbsp;		}
<b class="fc"><i>87</i>&nbsp;		String[] strPos = SEPARATOR_PATTERN.split(str.substring(1, str.indexOf(&#39;]&#39;)));</b>
<b class="fc"><i>88</i>&nbsp;		Integer[] sepPositions = new Integer[strPos.length];</b>
<b class="fc"><i>89</i>&nbsp;		for (int i = 0; i &lt; strPos.length; i++) {</b>
<b class="fc"><i>90</i>&nbsp;			sepPositions[i] = Integer.parseInt(strPos[i]);</b>
<i>91</i>&nbsp;		}
<i>92</i>&nbsp;
<b class="fc"><i>93</i>&nbsp;		String[] elements = new String[sepPositions.length];</b>
<b class="fc"><i>94</i>&nbsp;		str = str.substring(str.indexOf(&#39;]&#39;) + 1);</b>
<b class="fc"><i>95</i>&nbsp;		if (str.length() &lt; 3) {</b>
<b class="nc"><i>96</i>&nbsp;			return elements;</b>
<i>97</i>&nbsp;		}
<i>98</i>&nbsp;
<b class="fc"><i>99</i>&nbsp;		for (int i = 0, lastPos = 1; i &lt; sepPositions.length; i++) {</b>
<b class="fc"><i>100</i>&nbsp;			Integer sepPos = sepPositions[i];</b>
<b class="fc"><i>101</i>&nbsp;			elements[i] = str.substring(lastPos, lastPos + sepPos);</b>
<b class="fc"><i>102</i>&nbsp;			lastPos += sepPos + getSeparator().length();</b>
<i>103</i>&nbsp;		}
<b class="fc"><i>104</i>&nbsp;		return elements;</b>
<i>105</i>&nbsp;	}
<i>106</i>&nbsp;
<i>107</i>&nbsp;	@Override
<i>108</i>&nbsp;	public String targetToSource(C target) {
<b class="fc"><i>109</i>&nbsp;		StringBuilder builder = new StringBuilder();</b>
<b class="fc"><i>110</i>&nbsp;		appendLeadingSequence(builder);</b>
<i>111</i>&nbsp;
<b class="fc"><i>112</i>&nbsp;		boolean required = false;</b>
<b class="fc"><i>113</i>&nbsp;		List&lt;Integer&gt; separatorPositions = new ArrayList&lt;Integer&gt;();</b>
<b class="fc"><i>114</i>&nbsp;		for (Iterator&lt;E&gt; iterator = getElements(target); iterator.hasNext(); ) {</b>
<b class="fc"><i>115</i>&nbsp;			E elem = iterator.next();</b>
<b class="fc"><i>116</i>&nbsp;			Object identifier = ViennaUtils.SerializationUtils.save(ctx, elem);</b>
<b class="fc"><i>117</i>&nbsp;			String str = org.springframework.util.ClassUtils.getQualifiedName(elem.getClass()) + Serializer.CLASS_OBJECT_SEPARATOR + identifier;</b>
<b class="fc"><i>118</i>&nbsp;			builder.append(str).append(getSeparator());</b>
<i>119</i>&nbsp;
<b class="fc"><i>120</i>&nbsp;			separatorPositions.add(str.length());</b>
<b class="fc"><i>121</i>&nbsp;			required = required || requireIndices(str);</b>
<b class="fc"><i>122</i>&nbsp;		}</b>
<i>123</i>&nbsp;
<b class="fc"><i>124</i>&nbsp;		if (separatorPositions.size() &gt; 1 &amp;&amp; required) {</b>
<b class="fc"><i>125</i>&nbsp;			builder.insert(0, separatorPositions.toString().replace(&quot; &quot;, &quot;&quot;));</b>
<i>126</i>&nbsp;		}
<b class="fc"><i>127</i>&nbsp;		return appendTrailingSequence(builder).toString();</b>
<i>128</i>&nbsp;	}
<i>129</i>&nbsp;
<i>130</i>&nbsp;	protected boolean requireIndices(String str) {
<b class="fc"><i>131</i>&nbsp;		return str.contains(getSeparator());</b>
<i>132</i>&nbsp;	}
<i>133</i>&nbsp;
<i>134</i>&nbsp;	protected abstract Iterator&lt;E&gt; getElements(C target);
<i>135</i>&nbsp;
<i>136</i>&nbsp;	protected StringBuilder appendLeadingSequence(StringBuilder builder) {
<b class="fc"><i>137</i>&nbsp;		return builder.append(containerBegin);</b>
<i>138</i>&nbsp;	}
<i>139</i>&nbsp;
<i>140</i>&nbsp;	protected final StringBuilder appendTrailingSequence(StringBuilder builder) {
<b class="fc"><i>141</i>&nbsp;		int pos = builder.lastIndexOf(getSeparator());</b>
<b class="fc"><i>142</i>&nbsp;		if (pos == builder.length() - getSeparator().length()) {</b>
<b class="fc"><i>143</i>&nbsp;			builder.delete(pos, builder.length());</b>
<i>144</i>&nbsp;		}
<b class="fc"><i>145</i>&nbsp;		return builder.append(containerEnd);</b>
<i>146</i>&nbsp;	}
<i>147</i>&nbsp;
<i>148</i>&nbsp;	protected final String stripContainerSequences(String str) {
<b class="fc"><i>149</i>&nbsp;		return str.startsWith(containerBegin) ? str.substring(containerBegin.length(), str.length() - containerEnd.length()) : str;</b>
<i>150</i>&nbsp;	}
<i>151</i>&nbsp;
<i>152</i>&nbsp;	@Override
<i>153</i>&nbsp;	public final String getSeparator() {
<b class="fc"><i>154</i>&nbsp;		return &quot;,&quot;;</b>
<i>155</i>&nbsp;	}
<i>156</i>&nbsp;
<i>157</i>&nbsp;	@Override
<i>158</i>&nbsp;	public void setViennaContext(ViennaContext viennaContext) {
<b class="fc"><i>159</i>&nbsp;		this.ctx = viennaContext;</b>
<b class="fc"><i>160</i>&nbsp;	}</b>
<i>161</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
